﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
using System.Xml;


namespace LicenseManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGenerateGuid_Click(object sender, EventArgs e)
        {
            this.txtGuid.Text = Guid.NewGuid().ToString();
        }

        private void btnGenerateFile_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + "data.xml");
            doc.SelectSingleNode("/root/ip").InnerText = "0.0.0.0";
            doc.SelectSingleNode("/root/ua").InnerText = "IE9.0";
            doc.SelectSingleNode("/root/isreg").InnerText = isRegister.Checked ? "1" : "0";
            doc.SelectSingleNode("/root/regtime").InnerText = DateTime.Now.ToString();
            doc.SelectSingleNode("/root/regid").InnerText = this.txtGuid.Text;
            doc.SelectSingleNode("/root/end").InnerText = dtpick.Value.ToString();

            doc.Save(AppDomain.CurrentDomain.BaseDirectory + "data_plain.xml");

            //DES Encrpt
            string savedtext = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "data_plain.xml");
            savedtext = SE.Encrypt(savedtext);

            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "data_des.dat", savedtext);
            //load file to sign
            string privateLey = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "PrivateKey.txt");
            byte[] bytes = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "data_des.dat");
            byte[] endata = RSASign(bytes, privateLey);

            byte[] combile = new byte[bytes.Length + endata.Length];
            endata.CopyTo(combile, 0);
            bytes.CopyTo(combile, endata.Length);
            File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "sign.txt", endata);
            File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "combile.txt", combile);
            MessageBox.Show("成功！");
        }

        private void btnKeyGen_Click(object sender, EventArgs e)
        {
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(2048);
            //将RSA算法的公钥导出到字符串PublicKey中，参数为false表示不导出私钥   
            string PublicKey = rsaProvider.ToXmlString(false);
            //将RSA算法的私钥导出到字符串PrivateKey中，参数为true表示导出私钥
            string PrivateKey = rsaProvider.ToXmlString(true);

            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "PrivateKey.txt", PrivateKey);
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "PublicKey.txt", PublicKey);
            MessageBox.Show("创建成功！");

        }

        public bool RSAVerify(byte[] data, string key, byte[] sign)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(key);
            try
            {
                return rsa.VerifyData(data, "SHA1", sign);
            }
            catch (Exception)
            {
                return false; ;
            }
        }
        public byte[] RSASign(byte[] data, string key)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(key);
            try
            {
                return rsa.SignData(data, "SHA1");
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnDecrpt_Click(object sender, EventArgs e)
        {
            byte[] sbytes = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "combile.txt");
            byte[] sign = new byte[256];
            byte[] sec=new byte[sbytes.Length-sign.Length];
            Buffer.BlockCopy(sbytes, 0, sign, 0, 256);
            Buffer.BlockCopy(sbytes, 256, sec, 0, sec.Length);
            string publickey = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "PublicKey.txt");
            //byte[] planbytes = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "data_des.txt");

            bool result = RSAVerify(sec, publickey, sign);
            MessageBox.Show(result.ToString());
        }
    }

}

